package org.example.controller;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.example.service.assign.AssignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/assign")
public class AssignController {

    @Autowired
    AssignService assignService;

    /** 部署流程定义 */
    @GetMapping("/deployWithClassPath")
    public String deployWithClassPath() {
        RepositoryService rs = ProcessEngines.getDefaultProcessEngine().getRepositoryService();
        Deployment deployment = rs.createDeployment()
                .name("请假流程")
                .addClasspathResource("processes/demo2.bpmn")
                .deploy();
        rs.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
        return deployment.getId();
    }

    /** 开启一个流程实例(分配人员) */
    @GetMapping("/startProcessAssign")
    public String startProcessAssign(@RequestParam(value = "userName") String userName,
                                     @RequestParam(value = "userNames") String userNames) {
        return assignService.startProcessAssign(userName, userNames);
    }

    /** 组员认领任务 */
    @GetMapping("/claimTask")
    public String claimTask(@RequestParam(value = "taskId") String taskId,
                            @RequestParam(value = "userName") String userName) {
        return assignService.claimTask(taskId, userName);
    }
}
